#!/bin/sh

if [ $# -ne 1 ] ; then
    echo "Usage: $0 <init_template>"
    exit 1
fi

INIT_TEMPLATE=${1}
SERVICE_FILE=`echo ${INIT_TEMPLATE} | sed 's/.init.in/.service/'`

# Get the variables defined in the init template
. ${INIT_TEMPLATE}

if [ -z "${NAME}" ] ; then
    echo "Variable NAME is not set!"
    exit 1
fi
if [ -z "${PROJECT_NAME}" ] ; then
    echo "Variable PROJECT_NAME is not set!"
    exit 1
fi
if [ -z "${DESC}" ] ; then
    echo "Variable DESC is not set!"
    exit 1
fi

if [ -z "${SCRIPTNAME}" ] ; then
	SCRIPTNAME=/etc/init.d/${NAME}
fi
if [ -z "${SYSTEM_USER}" ] ; then
	SYSTEM_USER=${PROJECT_NAME}
fi
if [ -z "${SYSTEM_GROUP}" ] ; then
	SYSTEM_GROUP=${PROJECT_NAME}
fi
if [ -z "${SYSTEMD_TYPE}" ] ; then
	SYSTEMD_TYPE=simple
fi

# Find out what should go in After=
SHOULD_START=$(cat ${INIT_TEMPLATE} | grep "# Should-Start:" | sed 's/# Should-Start://')
X_START_BEFORE=$(cat ${INIT_TEMPLATE} | grep "# X-Start-Before:" | sed 's/# X-Start-Before://')
REQUIRED_START=$(cat ${INIT_TEMPLATE} | grep "# Required-Start:" | sed 's/# Required-Start://')

if [ -n "${SHOULD_START}" ] || [ -n "${REQUIRED_START}" ] ; then
	AFTER="After="
	WANTS="Wants="
fi

if [ -n "${SHOULD_START}" ] ; then
	for i in ${SHOULD_START} ; do
		AFTER="${AFTER}${i}.service "
		WANTS="${WANTS}${i}.service "
	done
fi

for i in $(cat ${INIT_TEMPLATE} | grep "# Required-Start:" | sed 's/# Required-Start://' ) ; do
	# Translate Required-Start: that has a $ ...
	if [ $( echo $i | head -c 1) = '$' ] ; then
		if [ "${i}" = '$network' ] ; then
			AFTER="${AFTER}network-online.target "
			WANTS="${WANTS}network-online.target "
		fi
		if [ "${i}" = '$local_fs' ]; then
			AFTER="${AFTER}local-fs.target "
		fi
		if [ "${i}" = '$remote_fs' ]; then
			AFTER="${AFTER}remote-fs.target "
		fi
	# Write down all of the others
	else
		AFTER="${AFTER}${i}.service "
		WANTS="${WANTS}${i}.service "
	fi
done
if [ "${WANTS}" = "Wants=" ]; then
	WANTS=""
fi

if [ -n "${X_START_BEFORE}" ] ; then
	BEFORE="Before="
	for i in ${X_START_BEFORE} ; do
		BEFORE="${BEFORE}${i}.service "
	done
fi

if [ "${SYSTEMD_TYPE}" = "oneshot" ] ; then
	SYSTEMD_RESTART="Restart=no"
else
	SYSTEMD_RESTART="Restart=on-failure"
fi

if [ -n "${SYSTEMD_DOC}" ] ; then
	SYSTEMD_DOC="Documentation=$SYSTEMD_DOC"
else
	SYSTEMD_DOC="Documentation=man:${NAME}(1)"
fi

if [ -n "${SYSTEMD_NOTIFY_ACCESS}" ] ; then
	SYSTEMD_NOTIFY_ACCESS="NotifyAccess=${SYSTEMD_NOTIFY_ACCESS}"
fi

echo "[Unit]
Description=${DESC}
$AFTER
$WANTS
$BEFORE
$SYSTEMD_DOC
$CUSTOM_SYSTEMD_UNIT

[Service]
User=${SYSTEM_USER}
Group=${SYSTEM_GROUP}
Type=${SYSTEMD_TYPE}
WorkingDirectory=~
RuntimeDirectory=${PROJECT_NAME} lock/${PROJECT_NAME}
CacheDirectory=${PROJECT_NAME}
ExecStart=${SCRIPTNAME} systemd-start
$SYSTEMD_RESTART
LimitNOFILE=65535
TimeoutStopSec=15
${SYSTEMD_NOTIFY_ACCESS}

[Install]
WantedBy=multi-user.target
" >${SERVICE_FILE}.tmp

# 1. Make several blank lines between blocks to single line
# 2. Remove last empty line
awk 'BEGIN{bl=0}/^$/{bl++;if(bl==1)print;else next}/^..*$/{bl=0;print}' ${SERVICE_FILE}.tmp | awk 'NR > 1{print t} {t = $0}END{if (NF) print }' > ${SERVICE_FILE}
rm -f ${SERVICE_FILE}.tmp
